package hos.demo

import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import hos.demo.ui.theme.KotlinUITheme
import hos.ksl.click
import hos.ksl.createBottomSheetDialogFragment
import hos.ksl.createDialog
import hos.ksl.createDialogFragment
import hos.ksl.createDialogLayout
import hos.ksl.show
import hos.ksl.showDialog
import hos.ksl.showDialogFragment

class MainActivity : AppCompatActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            KotlinUITheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Column(modifier = Modifier.padding(innerPadding)) {
                        TextButton(onClick = {
                            createDialog {
                                setTitle("温馨提示")
                                setMessage("这是一个原生弹窗")
                                show()
                            }
                        }) {
                            Text(text = "原生弹窗")
                        }
                        TextButton(onClick = {
                            createDialog {
                                setTitle("温馨提示")
                                setMessage("这是一个原生弹窗")
                                setNegativeButton("确定") { _, _ ->
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.showDialogFragment(supportFragmentManager)
                        }) {
                            Text(text = "原生弹窗Fragment")
                        }
                        TextButton(onClick = {
                            createDialogLayout(R.layout.dialog_tip) {
//                                it.showDialog()
                                click<LinearLayoutCompat> {
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.showDialog()
                        }) {
                            Text(text = "自定义弹窗")
                        }
                        TextButton(onClick = {
                            createDialogFragment(R.layout.dialog_tip) {
                                click<LinearLayoutCompat> {
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.show(supportFragmentManager)
                        }) {
                            Text(text = "自定义弹窗Fragment")
                        }
                        TextButton(onClick = {
                            createDialogLayout(R.layout.dialog_tip) {
                                click<LinearLayoutCompat> {
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.showDialog(gravity = Gravity.BOTTOM)
                        }) {
                            Text(text = "自定义弹窗底部")
                        }
                        TextButton(onClick = {
                            createDialogFragment(R.layout.dialog_tip, gravity = Gravity.BOTTOM) {
                                click<LinearLayoutCompat> {
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.show(supportFragmentManager)
                        }) {
                            Text(text = "自定义弹窗Fragment")
                        }
                        TextButton(onClick = {
//                            createBottomSheetDialog(R.layout.dialog_tip) {
//                                click<LinearLayoutCompat> {
//                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
//                                        .show()
//                                }
//                            }.show()
                        }) {
                            Text(text = "BottomSheetDialog")
                        }
                        TextButton(onClick = {
                            createBottomSheetDialogFragment(R.layout.dialog_tip) {
                                click<LinearLayoutCompat> {
                                    Toast.makeText(context, "点击事件", Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }.show(supportFragmentManager)
                        }) {
                            Text(text = "BottomSheetDialogFragment")
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    KotlinUITheme {
        Greeting("Android")
    }
}